package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.bean.PatrolPic;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.List;

public class NzdPicDetailActivity extends Activity implements View.OnClickListener {
    private RelativeLayout btnBack;
    private ViewPager viewpager;
    private int position;
    private List<PatrolPic> photos;
    private ViewPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_detail);
        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        viewpager = findViewById(R.id.viewpager);
        Intent intent = getIntent();
        position = intent.getIntExtra("position", 0);
        photos = (List<PatrolPic>) getIntent().getSerializableExtra("photos");
        adapter = new ViewPagerAdapter();
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(position);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnBack:
                finish();
                break;
        }
    }

    public class ViewPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return photos.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            String imgUrl = photos.get(position).getAttach_url();
            RelativeLayout view = (RelativeLayout) LayoutInflater.from(NzdPicDetailActivity.this).inflate(R.layout.viewpager_pic_item_s, null);
            PhotoView img = view.findViewById(R.id.photo_view);
//            TextView tv_date = view.findViewById(R.id.tv_date);
//            TextView tv_longitude = view.findViewById(R.id.tv_longitude);
//            TextView tv_latitude = view.findViewById(R.id.tv_latitude);
//            TextView tv_addr = view.findViewById(R.id.tv_addr);
            LinearLayout ll_bottom = view.findViewById(R.id.ll_bottom);
            ll_bottom.setVisibility(View.INVISIBLE);
            Glide.with(NzdPicDetailActivity.this)
                    .load(imgUrl)
                    .into(img);
//            tv_date.setText(photos.get(position).getAdd_time());
//            tv_longitude.setText(photos.get(position).getCreate_longitude() + "");
//            tv_latitude.setText(photos.get(position).getCreate_latitude() + "");
//            tv_addr.setText(photos.get(position).getAddr());
            container.addView(view);
            return view;
        }
    }
}
